package org.dftproject.genesis.data.genealogy.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.dftproject.genesis.data.genealogy.GenealogyUtils;
import org.dftproject.genesis.data.genealogy.IName;
import org.dftproject.genesis.data.genealogy.IPerson;
import org.dftproject.genesis.data.genealogy.IRole;
import org.dftproject.genesis.data.impl.EntityImpl;

public class PersonImpl extends EntityImpl implements IPerson {

	private final Map<String, Set<IRole>> rolesByType = new HashMap<String, Set<IRole>>();

	public Set<IRole> getRoles() {
		Set<IRole> results = new HashSet<IRole>();
		
		for (Set<IRole> roles : rolesByType.values())
			results.addAll(roles);
		
		return Collections.unmodifiableSet(results);
	}
	
	public Set<IRole> getRoles(String type) {		
		Set<IRole> roles = rolesByType.get(type);
		if (roles != null)
			return Collections.unmodifiableSet(roles);
		return Collections.emptySet();
	}
	
	public Set<IRole> getRoles(String[] types) {		
		Set<IRole> results = new HashSet<IRole>();
		
		for (String type : types) {
			Set<IRole> roles = rolesByType.get(type);
			if (roles != null)
				results.addAll(roles);
		}
		
		return Collections.unmodifiableSet(results);
	}

	public void addRole(IRole role) {
		String name = role.getType();
		Set<IRole> roles = rolesByType.get(name);
		if (roles == null) {
			roles = new HashSet<IRole>();
			rolesByType.put(name, roles);
		}
		roles.add(role);
	}
	
	@Override
	public String toString() {
		IName name = GenealogyUtils.getName(this);
		if (name != null)
			return "Person(" + name + ")";
		return super.toString();
	}
	
}
